<?php
include_once('_PATH.php');
require_once($_CLASSPATH . '/persistence/UserPersistence.class.php');
require_once($_CLASSPATH . '/model/User.class.php');

class UserService {
	function createSocialUser( $_socialNetId , $_fbuid ) {
		try {
			$uid = getUserBySociaId( $_socialNetId , $_fbuid );
		} catch (Exception $e) {
			$user = & $this->createUser( 'FB_USER_' . $_fbuid );
			$uid = $user->uid;
		}

		$this->associateSocialUser( $uid, $_socialNetId, $_fbuid );
		return $uid;
	}

	function & createUser( $_uname ){
		$user = new User();
		$persist = new UserPersistence();
		$user->uid = $persist->createUser( $_uname );
		return $user;
	}

	function associateSocialUser( $_uid, $_socialNetId, $_fbuid) {
		$persist = new UserPersistence();
		$persist->associateSocialUser( $_uid , $_socialNetId , $_fbuid);
	}

	function getUserBySociaId( $_socialNetId , $_socialId ) {
		$persist = new UserPersistence();
		$uid = $persist->getUserBySociaId( $_socialNetId , $_socialId );
		if ( ! $uid )
			throw new Exception(ErrorCodes::ObjectNotFound);

		return $uid;
	}
}

?>